﻿Imports System.Data.SqlClient
Public Class DiaChiDAO
    Public Function SelectID(ByVal diachi As DiaChiDTO) As DiaChiDTO    
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from DIACHI where MaDiaChi = " + diachi.MaDiaChi.ToString()
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        da.Fill(data)
        Try
            diachi.MaDiaChi = Convert.ToInt32(data.Rows(0)("MaDiaChi").ToString())
            diachi.SoNha = data.Rows(0)("SoNha").ToString()
            diachi.Duong = data.Rows(0)("Duong").ToString()
            diachi.Phuong = data.Rows(0)("Phuong").ToString()
            diachi.Quan = data.Rows(0)("Quan").ToString()
        Catch ex As Exception

        End Try

        Return diachi
    End Function

    Public Function GetItemID(ByVal sonha As String, ByVal duong As String, ByVal phuong As String, ByVal quan As String) As DiaChiDTO
        Dim diachi As New DiaChiDTO
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from DIACHI where SoNha = '" + sonha + "'"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        da.Fill(data)
        Try
            diachi.MaDiaChi = Convert.ToInt32(data.Rows(0)("MaDiaChi").ToString())
            diachi.SoNha = data.Rows(0)("SoNha").ToString()
            diachi.Duong = data.Rows(0)("Duong").ToString()
            diachi.Phuong = data.Rows(0)("Phuong").ToString()
            diachi.Quan = data.Rows(0)("Quan").ToString()
        Catch ex As Exception

        End Try

        Return diachi
    End Function
    Public Function GetAll() As DataTable
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from DIACHI"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        Try
            da.Fill(data)
        Catch ex As Exception

        End Try
        Return data
    End Function
    Public Sub Insert(ByVal dc As DiaChiDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_InsertDIACHI @SoNha,@Duong,@Phuong,@Quan,@MaDiaChi"
            Dim para As SqlParameter = cmd.Parameters.Add("@SoNha", SqlDbType.VarChar)
            para.Value = dc.SoNha
            para = cmd.Parameters.Add("@Duong", SqlDbType.NVarChar)
            para.Value = dc.Duong
            para = cmd.Parameters.Add("@Phuong", SqlDbType.NVarChar)
            para.Value = dc.Phuong
            para = cmd.Parameters.Add("@Quan", SqlDbType.NVarChar)
            para.Value = dc.Quan
            para = cmd.Parameters.Add("@MaDiaChi", SqlDbType.Int)
            para.Value = dc.MaDiaChi
            cmd.ExecuteNonQuery()
            'MessageBox.Show("Địa chỉ đã được tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Update(ByVal dc As DiaChiDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_UpdateDIACHI @MaDiaChi,@SoNha,@Duong,@Phuong,@Quan"
            Dim para As SqlParameter = cmd.Parameters.Add("@SoNha", SqlDbType.VarChar)
            para.Value = dc.SoNha
            para = cmd.Parameters.Add("@Duong", SqlDbType.NVarChar)
            para.Value = dc.Duong
            para = cmd.Parameters.Add("@Phuong", SqlDbType.NVarChar)
            para.Value = dc.Phuong
            para = cmd.Parameters.Add("@Quan", SqlDbType.NVarChar)
            para.Value = dc.Quan
            para = cmd.Parameters.Add("@MaDiaChi", SqlDbType.Int)
            para.Value = dc.MaDiaChi
            cmd.ExecuteNonQuery()
            'MessageBox.Show("Địa chỉ đã được cập nhật thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Delete(ByVal dc As DiaChiDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_DeleteDIACHI @MaDiaChi"
            Dim para As SqlParameter = cmd.Parameters.Add("@MaDiaChi", SqlDbType.Int)
            para.Value = dc.MaDiaChi
            cmd.ExecuteNonQuery()
            'MessageBox.Show("Địa chỉ đã được xóa thành công", "Thông báo")
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
